package com.kedacom.freedomLabel.model.db;

import android.arch.persistence.room.TypeConverter;

import com.kedacom.freedomLabel.model.annotation.ItemCategory;
import com.kedacom.freedomLabel.model.bean.Template;

import java.util.UUID;

/**
 * Author：李程
 * CreateTime：2019/8/17 17:52
 * E-mail：licheng@kedacom.com
 * Describe：
 */
public final class Converters {

    public static final String[] sDefaultColumnNames = new String[]{
            ItemCategory.ITEM_1, ItemCategory.ITEM_2,
            ItemCategory.ITEM_3, ItemCategory.ITEM_4,
            ItemCategory.ITEM_5, ItemCategory.ITEM_6
    };

    private static final String COLUMN_SEPARATOR = "&";

    @TypeConverter
    public static String positionToString(Template.Position position) {
        if (position == null) return null;
        StringBuilder data = new StringBuilder();
        data.append(position.x).append(COLUMN_SEPARATOR)
                .append(position.y).append(COLUMN_SEPARATOR)
                .append(position.size);
        return data.substring(0, data.length());
    }

    @TypeConverter
    public static Template.Position stringToPosition(String value) {
        if (value == null) return null;
        String[] columnNames = value.split(COLUMN_SEPARATOR);
        return new Template.Position(Integer.parseInt(columnNames[0]),
                Integer.parseInt(columnNames[1]), Integer.parseInt(columnNames[2]));
    }

    public static String randomUUID() {
        return UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
    }

    public static int getItemColumnNamePosition(@ItemCategory String itemCategory) {
        int dataPosition;
        switch (itemCategory) {
            case ItemCategory.ITEM_1:
                dataPosition = 0;
                break;
            case ItemCategory.ITEM_2:
                dataPosition = 1;
                break;
            case ItemCategory.ITEM_3:
                dataPosition = 2;
                break;
            case ItemCategory.ITEM_4:
                dataPosition = 3;
                break;
            case ItemCategory.ITEM_5:
                dataPosition = 4;
                break;
            case ItemCategory.ITEM_6:
                dataPosition = 5;
                break;
            default:
                dataPosition = -1;
                break;
        }
        return dataPosition;
    }

    @TypeConverter
    public static String boolToStringArray(Boolean[] status) {
        if (status == null || status.length == 0) return null;
        StringBuilder data = new StringBuilder();
        for (Boolean enable : status) {
            if (enable == null || !enable) {
                data.append("0");
            } else {
                data.append("1");
            }
            data.append(COLUMN_SEPARATOR);
        }
        return data.substring(0, data.length() - 1);
    }

    @TypeConverter
    public static Boolean[] stringArrayToBool(String status) {
        if (status == null || status.length() == 0) {
            return new Boolean[]{false, false, false, false, false, false};
        }
        String[] data = status.split(COLUMN_SEPARATOR);
        Boolean[] result = new Boolean[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = data[i].equals("1");
        }
        return result;
    }

    @TypeConverter
    public static String columnNamesToString(String[] columnNames) {
        if (columnNames == null || columnNames.length == 0) return null;
        StringBuilder data = new StringBuilder();
        for (String columnName : columnNames) {
            data.append(columnName).append(COLUMN_SEPARATOR);
        }
        return data.substring(0, data.length() - 1);
    }

    @TypeConverter
    public static String[] stringToColumnNames(String value) {
        if (value == null) return sDefaultColumnNames;
        String[] columnNames = value.split(COLUMN_SEPARATOR);
        int length = columnNames.length;
        int defaultLength = sDefaultColumnNames.length;
        if (length < defaultLength) {
            String[] data = new String[defaultLength];
            System.arraycopy(columnNames, 0, data, 0, length);
            System.arraycopy(sDefaultColumnNames, length - 1, data, length - 1,
                    defaultLength - length - 1);
            return data;
        }
        return columnNames;
    }
}